home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Printer Drivers… / CustomWriterGX / GlobalData.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  5.1 KB  |  164 lines  |  [TEXT/MPS ]

  1. /*
  2.     FILENAME
  3.         GlobalData.c
  4.  
  5.     DESCRIPTION
  6.         Contains code that shows a prefered way to manage unique global
  7.         data for different message handler instances.  This method
  8.         stores all globals off of the message manager instance context.
  9.         It has the advantage that it will work under any compiler.
  10.         This method is documented in detail in "Inside Macintosh: GX
  11.         Environment & Utilities" starting on page 6-10
  12.         
  13.     COPYRIGHT
  14.         Copyright © 1995 Apple Computer, Inc.
  15.         All rights reserved.
  16.     
  17.     Modification history
  18.         06/14/96 - cn               -    Updated to support Universal Interfaces 2.1.
  19.         
  20.         10/04/95 - David Hayward -    Version 1.0.4 modified code so that
  21.                                     the driver can be build under MPW,
  22.                                     Metrowerks, and Symantec.  In general,
  23.                                     all that was required to do this was 
  24.                                     to add an inline-assembly jumptable
  25.                                     and to store all globals off of the
  26.                                     message manager instance context.
  27.                                     Also made a few changes so that the
  28.                                     driver can be rebuilt to support any
  29.                                     resolution by changing the #defines
  30.                                     kResolution and kPatStretch in
  31.                                     "CommonDefines.h"
  32.  
  33.         06/14/95 - Dave Hersey -    Version 1.0.3 to fix a bug in
  34.                                     CustomBufferingAndIO.c when creating
  35.                                     high-res PICTs, and to make the size
  36.                                     of buffers more flexible.
  37.  
  38.         05/26/95 - Dave Hersey -    Version 1.0.2 to add the new 'outp'
  39.                                     desktop printer resource in NewApp.c.
  40.  
  41.         05/03/95 - Dave Hersey -    Version 1.0.1 to fix some minor bugs in
  42.                                     CustomBufferingAndIO.c.
  43.  
  44.         01/14/95 - Dave Hersey -    Begat.
  45. */
  46.  
  47.  
  48. #include "GlobalData.h"
  49.  
  50.  
  51. /*    -----------------------------------------------------------------------
  52.  
  53.     GetGlobals returns the DriverGlobals structure
  54.     whose handle is stored in the MessageHandler InstanceContext.
  55.  
  56.     -----------------------------------------------------------------------    */
  57.  
  58. DriverGlobalsHdl GetGlobals ( void )
  59. {
  60.     DriverGlobalsHdl dataHandle;
  61.     
  62.     dataHandle = (DriverGlobalsHdl) GetMessageHandlerInstanceContext();
  63.  
  64.     return dataHandle;
  65. }
  66.  
  67.  
  68. /*    -----------------------------------------------------------------------
  69.  
  70.     CreateAndStoreGlobals is a routine we call to initialize and store a
  71.     handle in the are we allocated at the end of our NewApp.a jump table.
  72.     
  73.     This routine and its counterpart (DisposeGlobals) provide an alternate
  74.     method of handling global data in a driver.  The two calls provide a
  75.     method similar to GetMessageHandlerInstanceContext and
  76.     SetMessageHandlerInstanceContext, except that this method can be used
  77.     even if your code didn't receive control via a message override.  For
  78.     example, if your code is being executed from a dialog manager
  79.     callback.  The standard global data handling routines that are provided
  80.     with QuickDraw GX will only work if your code received control via a
  81.     message override.
  82.  
  83.     -----------------------------------------------------------------------    */
  84.  
  85. OSErr CreateGlobals ( void )
  86. {
  87.     OSErr                err;
  88.     DriverGlobalsHdl    dataHandle;
  89.  
  90. #if 0
  91.     DriverGlobals        defaultGlobals = {    0x12345678,
  92.                                             0,            // lineFeeds - accumulated lines feeds
  93.                                             0,            // pagesImaged - Number of pages imaged.
  94.                                             0,            // lastYPos - Used by our RasterDatIn override.
  95.                                             0,            /// curFileRefNum - refNum of current open file.
  96.                                             0,            // pixMapRowBytes - rowBytes of the pixmap we create.
  97.                                             {0,0,0,0},    // pixMapBounds - Bounds of the pixmap we create.
  98.                                             0,            // hRes - Horizontal output resolution.
  99.                                             0,            // vRes - Vertical output resolution.
  100.                                             {0,0,"\p"},    // fileLocation - Where to put our files.
  101.                                             nil            // buffer - 
  102.                                           } ;
  103. #endif
  104.  
  105.     /*
  106.     Create a new temporary memory handle, initialize
  107.     it, and store it as the message handler's instance
  108.     context.
  109.     */
  110.     
  111.     dataHandle = (DriverGlobalsHdl) TempNewHandle(sizeof(DriverGlobals),&err);
  112.     
  113.     if (err == noErr)
  114.     {
  115.         (**dataHandle).temp                    = 0x12345678;
  116.         (**dataHandle).lineFeeds            = 0;
  117.         (**dataHandle).pagesImaged            = 0;
  118.         (**dataHandle).lastYPos                = 0;
  119.         (**dataHandle).curFileRefNum        = 0;
  120.         (**dataHandle).pixMapRowBytes        = 0;
  121.         (**dataHandle).pixMapBounds.top        = 0;
  122.         (**dataHandle).pixMapBounds.left    = 0;
  123.         (**dataHandle).pixMapBounds.bottom    = 0;
  124.         (**dataHandle).pixMapBounds.right    = 0;
  125.         (**dataHandle).hRes                    = 0;
  126.         (**dataHandle).vRes                    = 0;
  127.         (**dataHandle).fileLocation.vRefNum    = 0;
  128.         (**dataHandle).fileLocation.parID    = 0;
  129.         (**dataHandle).fileLocation.name[0]    = '\0';
  130.         (**dataHandle).buffer                = nil;
  131.         SetMessageHandlerInstanceContext(dataHandle);
  132.     }
  133.     
  134.     return err;
  135. }
  136.  
  137.  
  138. /*    -----------------------------------------------------------------------
  139.  
  140.     DisposeGlobals is a routine that disposes of the global data we stored
  141.     in our jump table's data area via CreateAndStoreGlobals.
  142.  
  143.     -----------------------------------------------------------------------    */
  144.  
  145. void DisposeGlobals ( void )
  146. {
  147.     DriverGlobalsHdl dataHandle;
  148.     
  149.     /*
  150.     Retrieve the message handler's instance context. If the
  151.     value returned isn't nil, it's a handle that we stored
  152.     earlier. Dispose of the handle and set the instance
  153.     context to nil to "clear" it.
  154.     */
  155.     
  156.     dataHandle = (DriverGlobalsHdl) GetMessageHandlerInstanceContext();
  157.     
  158.     if (dataHandle != nil)
  159.     {
  160.         DisposeHandle( (Handle)dataHandle );
  161.         SetMessageHandlerInstanceContext(nil);
  162.     }
  163. }
  164.